﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SWSoft.Framework;
using System.Reflection;
using System.Diagnostics;
using System.Timers;
using MediSIIM.BizComponent.HangZhouSYKT;
using System.Xml;
using System.IO;

namespace HangZhouSYKT
{
    public partial class FormSYKT : Form
    {
        public FormSYKT()
        {
            InitializeComponent();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                button1_Click(sender, null);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            BindData(dataGridView1, ep.GetTable(comboBox1.SelectedIndex == 0 ? EntryPoint.SQL001 : EntryPoint.SQL002));
        }
        EntryPoint ep = new EntryPoint();
        private void FormSYKT_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 1;
            if (File.Exists(Application.StartupPath + "\\config.ini"))
            {
                using (var sw = new StreamReader(Application.StartupPath + "\\config.ini", Encoding.UTF8))
                {
                    var line = sw.ReadLine().Split('=');
                    if (line.Length == 2)
                    {
                        txtywzqhm.Text = line[1];
                    }
                }

            }
        }


        public void BindData(DataGridView view, DataTable table)
        {
            view.DataSource = table;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var item = dataGridView1.SelectedRows[0].DataBoundItem as DataRowView;
            string cmdText = "";
            if (comboBox1.SelectedIndex == 0)
            {
                cmdText = "insert into yb_mzjsmx(mzjsid,jsmxxh,jsmxfs,jsmxdm,jsywmc,jszwmc,jsmxje,jsmxnr)\r\n";
            }
            else
            {
                cmdText = "insert into yb_zyjsmx(zyjsid,	jsmxxh,jsmxfs,jsmxdm,jsywmc,jszwmc,jsmxje,jsmxnr)\r\n";
            }
            foreach (var jg in EntryPoint.GetOut(item["YBJSXX"] + "", item["YBBRXX"] + ""))
            {
                if (comboBox1.SelectedIndex == 0)
                {
                    #region 门诊
                    var mx = new YB_MZJSMX();
                    mx.MZJSID = decimal.Parse(item["MZJSID"].ToString());
                    mx.JSMXXH = Convert.ToDecimal(jg.JIEGUOXH);
                    mx.JSMXDM = jg.JIEGUODM;
                    mx.JSMXFS = Convert.ToDecimal(jg.JIEGUOLX);
                    mx.JSYWMC = jg.JIEGUODM;
                    mx.JSZWMC = jg.JIEGUOMC;
                    if (mx.JSMXFS.ToString() == "0")
                    {
                        mx.JSMXNR = jg.JIEGUONR;
                    }
                    else
                    {
                        mx.JSMXJE = EntryPoint.GetDecimal(jg.JIEGUONR);
                    }

                    cmdText += string.Format("SELECT {0}  FROM DUAL UNION ", mx.GetValues());
                    #endregion
                }
                else
                {
                    #region 住院
                    var mx = new YB_MZJSMX();
                    mx.MZJSID = decimal.Parse(item["ZYJSID"].ToString());
                    mx.JSMXXH = Convert.ToDecimal(jg.JIEGUOXH);
                    mx.JSMXDM = jg.JIEGUODM;
                    mx.JSMXFS = Convert.ToDecimal(jg.JIEGUOLX);
                    mx.JSYWMC = jg.JIEGUODM;
                    mx.JSZWMC = jg.JIEGUOMC;
                    if (mx.JSMXFS.ToString() == "0")
                    {
                        mx.JSMXNR = jg.JIEGUONR;
                    }
                    else
                    {
                        mx.JSMXJE = EntryPoint.GetDecimal(jg.JIEGUONR);
                    }
                    cmdText += string.Format("SELECT {0}  FROM DUAL UNION ", mx.GetValues());
                    #endregion
                }
            }
            cmdText = cmdText.Remove(cmdText.Length - 6);
            ep.executesql(cmdText);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            using (var sw = new StreamWriter(Application.StartupPath + "\\config.ini", false, Encoding.UTF8))
            {
                sw.WriteLine(string.Format("ywzqhm={0}", txtywzqhm.Text));
            }
            HangZhouSYKTradeParms callparam = new HangZhouSYKTradeParms(1601, "", "2006", txtywzqhm.Text, "DBA");
            callparam["KIND"] = "07";
            callparam["TYPE"] = "01";
            callparam["AAE035"] = "";
            callparam["AAE030"] = "";
            callparam["AAE031"] = "";
            callparam["AKC190"] = "";
            YiBaoJKDLL.YBCall(callparam);
            var fileinfo = new FileInfo(callparam.OutItems["FILENAME"]);
            if (fileinfo.Exists)
            {
                using (var sw = new StreamWriter(Application.StartupPath + "\\Item.xml", false, Encoding.UTF8))
                {
                    sw.Write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><DocumentElement>");
                    sw.Write(File.ReadAllText(fileinfo.FullName, Encoding.Default));
                    sw.Write("</DocumentElement>");
                }
                button4_Click(sender, e);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var table = new DataTable("OUTPUT");
            table.Columns.Add("AKC275");
            table.Columns.Add("AKC276");
            table.Columns.Add("AAC001");
            table.Columns.Add("AKA130");
            table.Columns.Add("AKC021");
            table.Columns.Add("AKB020");
            table.Columns.Add("AKC020");
            table.Columns.Add("AKC190");
            table.Columns.Add("AKC196");
            table.Columns.Add("AKC141");
            table.Columns.Add("AKC701");
            table.Columns.Add("AKC195");
            table.Columns.Add("AKC194");
            table.Columns.Add("AAE040");
            table.Columns.Add("AAE072");
            table.Columns.Add("AKA135");
            table.Columns.Add("AKC264");
            table.Columns.Add("AKC702");
            table.Columns.Add("AKC255");
            table.Columns.Add("AKC703");
            table.Columns.Add("AKC260");
            table.Columns.Add("AKC706");
            table.Columns.Add("AKC707");
            table.Columns.Add("AKC708");
            table.Columns.Add("AKC709");
            table.Columns.Add("AKC710");
            table.Columns.Add("AKC261");
            table.Columns.Add("AKC263");
            table.Columns.Add("AKC228");
            table.Columns.Add("AKC253");
            table.Columns.Add("AKC256");
            table.Columns.Add("AKC711");
            table.Columns.Add("AKC712");
            table.Columns.Add("AKC090");
            table.Columns.Add("AKC740");
            table.Columns.Add("AKC741");
            table.Columns.Add("AKC742");
            table.Columns.Add("AKC743");
            table.Columns.Add("AKC744");
            table.Columns.Add("AKC745");
            table.Columns.Add("AAC002");
            table.Columns.Add("AAC003");
            table.Columns.Add("AAC004");
            table.Columns.Add("AAC006");
            table.Columns.Add("AAB001");
            table.Columns.Add("AAB004");
            table.Columns.Add("AKC200");
            table.Columns.Add("AKC801");
            table.Columns.Add("AKC803");
            //table.WriteXml(Application.StartupPath + "\\Item1.xml");
            table.ReadXml(Application.StartupPath + "\\Item.xml");
            table.Columns["AKC275"].ColumnName = "发送方报文";
            table.Columns["AKC276"].ColumnName = "接受方报文";
            table.Columns["AAC001"].ColumnName = "个人编号";
            table.Columns["AKA130"].ColumnName = "医疗类别";
            table.Columns["AKC021"].ColumnName = "人员类别";
            table.Columns["AKB020"].ColumnName = "机构编码";
            table.Columns["AKC190"].ColumnName = "住院流水号";
            table.Columns["AKC196"].ColumnName = "出院疾病编码";
            table.Columns["AKC141"].ColumnName = "出院疾病名称";
            table.Columns["AKC701"].ColumnName = "出院描述";
            table.Columns["AKC195"].ColumnName = "出院原因";
            table.Columns["AKC194"].ColumnName = "出院日期";
            table.Columns["AAE040"].ColumnName = "结算日期";
            table.Columns["AAE072"].ColumnName = "单据号";
            table.Columns["AKA135"].ColumnName = "交易类型";
            table.Columns["AKC264"].ColumnName = "医疗费总额";
            table.Columns["AKC702"].ColumnName = "医保基金支付总金额";
            table.Columns["AKC255"].ColumnName = "本年帐户支付	";
            table.Columns["AKC703"].ColumnName = "历年帐户支付	";
            table.Columns["AKC260"].ColumnName = "统筹基金支付	";
            table.Columns["AKC706"].ColumnName = "大病救助基金	";
            table.Columns["AKC707"].ColumnName = "社会救助支付	";
            table.Columns["AKC708"].ColumnName = "公务员补助支付	";
            table.Columns["AKC709"].ColumnName = "民政优抚基金	";
            table.Columns["AKC710"].ColumnName = "民政补助	";
            table.Columns["AKC261"].ColumnName = "现金支付	";
            table.Columns["AKC263"].ColumnName = "符合基本医疗保险费用";
            table.Columns["AKC228"].ColumnName = "自理费用";
            table.Columns["AKC253"].ColumnName = "自费费用";
            table.Columns["AKC256"].ColumnName = "本次住院起付标准";
            table.Columns["AKC711"].ColumnName = "本次门诊起付标准";
            table.Columns["AKC712"].ColumnName = "门诊起付标准累计";
            table.Columns["AKC090"].ColumnName = "住院次数";
            table.Columns["AKC740"].ColumnName = "分段自负1";
            table.Columns["AKC741"].ColumnName = "分段自负2";
            table.Columns["AKC742"].ColumnName = "分段自负3";
            table.Columns["AKC743"].ColumnName = "分段自负4";
            table.Columns["AKC744"].ColumnName = "分段自负5";
            table.Columns["AKC745"].ColumnName = "分段自负6";
            table.Columns["AAC002"].ColumnName = "身份证号";
            table.Columns["AAC003"].ColumnName = "姓名";
            table.Columns["AAC004"].ColumnName = "性别";
            table.Columns["AAC006"].ColumnName = "出生日期";
            table.Columns["AAB001"].ColumnName = "单位编码";
            table.Columns["AAB004"].ColumnName = "单位名称";
            table.Columns["AKC200"].ColumnName = "附加信息";
            table.Columns["AKC801"].ColumnName = "就医地代码";
            table.Columns["AKC803"].ColumnName = "参保地代码";
            table.Columns["AKC020"].ColumnName = "卡号";
            dataGridView2.DataSource = table;
        }

        public int gettradeno()
        {
            return int.Parse(comboBox2.SelectedItem.ToString().Split('.')[0]);
        }

        private void button5_Click(object sender, EventArgs e)
        {

            try
            {
                HangZhouSYKTradeParms callparam = new HangZhouSYKTradeParms(gettradeno(), "", "2006", txtywzqhm.Text, "DBA");
                callparam.AKC803 = canbaodi;
                callparam.AKC020 = kahao;
                callparam.ORGMSGID = ORGMSGID;
                callparam.ORGMSGNO = ORGMSGNO;
                foreach (var item in txtindata.Text.Split('|'))
                {
                    var strs = item.Split('=');
                    if (strs.Length == 2)
                    {
                        callparam[strs[0]] = strs[1];
                    }
                }
                YiBaoJKDLL.YBCall(callparam);
                txtoutdata.Text = callparam.OUTPUT;
                dataGridView3.DataSource = callparam.OutTable;
            }
            catch (Exception ex)
            {
                txtoutdata.Text = ex.Message;
            }
        }

        private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                //添加行号 
                SolidBrush v_SolidBrush = new SolidBrush((sender as DataGridView).RowHeadersDefaultCellStyle.ForeColor);
                int v_LineNo = 0;
                v_LineNo = e.RowIndex + 1;

                string v_Line = v_LineNo.ToString();

                e.Graphics.DrawString(v_Line, e.InheritedRowStyle.Font, v_SolidBrush, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 5);

            }
            catch (Exception ex)
            {
            }
        }
        string kahao;
        string canbaodi;
        string ORGMSGNO;
        string ORGMSGID;
        private void 门诊退号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView2.SelectedRows.Count > 0)
            {
                var row = dataGridView2.SelectedRows[0].DataBoundItem as DataRowView;
                canbaodi = row["参保地代码"].ToString();
                ORGMSGID = row["发送方报文"].ToString();
                ORGMSGNO = "1105";
                kahao = row["卡号"].ToString();
                txtindata.Text = string.Format("AKC276={0}|AKC190={1}|AAE011=DBA", row["接受方报文"], row["住院流水号"]);
            }
        }
    }
}
